package com.yunfeisoft.controller.business;

import com.applet.base.BaseController;
import com.applet.utils.*;
import com.yunfeisoft.business.model.CmsSubject;
import com.yunfeisoft.business.model.CmsSubjectProductRelation;
import com.yunfeisoft.business.service.inter.CmsSubjectProductRelationService;
import com.yunfeisoft.business.service.inter.CmsSubjectService;
import com.yunfeisoft.enumeration.YesNoEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: CmsSubjectController
 * Description: 专题表Controller
 * Author: Jackie liu
 * Date: 2020-06-02
 */
@Controller
public class CmsSubjectController extends BaseController {

    @Autowired
    private CmsSubjectService cmsSubjectService;
    @Autowired
    private CmsSubjectProductRelationService cmsSubjectProductRelationService;

    /**
     * 添加专题表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/cmsSubject/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(CmsSubject record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "categoryId", "分类为空");
        validator.required(request, "title", "标题为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        record.setId(KeyUtils.getKey());
        String productIds = ServletRequestUtils.getStringParameter(request, "productIds", null);
        if (StringUtils.isNotBlank(productIds)) {
            String[] proIds = productIds.split(",");
            List<CmsSubjectProductRelation> subjectProductList = new ArrayList<>();
            for (String proId : proIds) {
                CmsSubjectProductRelation productRelation = new CmsSubjectProductRelation();
                productRelation.setProductId(proId);
                productRelation.setSubjectId(record.getId());
                subjectProductList.add(productRelation);
            }
            record.setSubjectProductList(subjectProductList);
        }
        record.setCollectCount(0);
        record.setCommentCount(0);
        record.setForwardCount(0);
        record.setProductCount(0);
        record.setReadCount(0);

        cmsSubjectService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改专题表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/cmsSubject/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(CmsSubject record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "categoryId", "分类为空");
        validator.required(request, "title", "标题为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        String productIds = ServletRequestUtils.getStringParameter(request, "productIds", null);
        if (StringUtils.isNotBlank(productIds)) {
            String[] proIds = productIds.split(",");
            List<CmsSubjectProductRelation> subjectProductList = new ArrayList<>();
            for (String proId : proIds) {
                CmsSubjectProductRelation productRelation = new CmsSubjectProductRelation();
                productRelation.setProductId(proId);
                productRelation.setSubjectId(record.getId());
                subjectProductList.add(productRelation);
            }
            record.setSubjectProductList(subjectProductList);
        }
        cmsSubjectService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询专题表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/cmsSubject/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        CmsSubject record = cmsSubjectService.load(id);
        if (record != null) {
            List<CmsSubjectProductRelation> relationList = cmsSubjectProductRelationService.queryBySubjectId(id);
            record.setSubjectProductList(relationList);
        }
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询专题表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/cmsSubject/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String title = ServletRequestUtils.getStringParameter(request, "title", null);

        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("title", title);

        Page<CmsSubject> page = cmsSubjectService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除专题表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/cmsSubject/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        cmsSubjectService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }

    /**
     * 显示
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/cmsSubject/acceptShowStatus", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response acceptShowStatus(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        cmsSubjectService.modifyShowStatus(id, YesNoEnum.YES_ACCPET.getValue());
        return ResponseUtils.success("操作成功");
    }

    /**
     * 不显示
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/cmsSubject/cancelShowStatus", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response cancelShowStatus(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        cmsSubjectService.modifyShowStatus(id, YesNoEnum.NO_CANCEL.getValue());
        return ResponseUtils.success("操作成功");
    }

    /**
     * 推荐
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/cmsSubject/acceptRecommendStatus", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response acceptRecommendStatus(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        cmsSubjectService.modifyRecommendStatus(id, YesNoEnum.YES_ACCPET.getValue());
        return ResponseUtils.success("操作成功");
    }

    /**
     * 不推荐
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/cmsSubject/cancelRecommendStatus", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response cancelRecommendStatus(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        cmsSubjectService.modifyRecommendStatus(id, YesNoEnum.NO_CANCEL.getValue());
        return ResponseUtils.success("操作成功");
    }
}
